package com.example.studentmanagersystem;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.studentmanagersystem.R;
import com.fs.adapters.StuSelectClassesAdapter;
import com.fs.po.ClassTec;
import com.fs.po.Classes;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class StudentSelectClassesActivity extends Activity {

	
	private ListView classesTecList;
	
	private Button btn;
	
	private List<ClassTec> List=new ArrayList<ClassTec>();
	
	private StuSelectClassesAdapter selectClassesAdapter;
	
	private Button actionBack,actionMenu;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.student_select_classes);
		
		init();
		
	}
	
	/**
	 * 初始化
	 */
	private void init() {
		this.classesTecList=(ListView) this.findViewById(R.id.student_select_class_ListView);
		
		this.btn=(Button) this.findViewById(R.id.student_select_btn);
		
		this.actionBack=(Button) this.findViewById(R.id.stuClass_action_backButton);
		this.actionMenu=(Button) this.findViewById(R.id.stuClass_action_menu);
		
		this.selectClassesAdapter=new StuSelectClassesAdapter(this);
		
		this.classesTecList.setAdapter(selectClassesAdapter);
		setListener();

	}
	
	
	private void setListener() {
		this.btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
//				String sql="select data.class_id from data,stu,tec where stu._id=data.stu_id and tec._id=data.tec_id and tec.class_id=data.class_id";
//				
//				submitCmd(URL.SOCRE_URL, "stu_get_selected_class",sql );
//				selectClassesAdapter.notifyDataSetChanged();
				getData("stu_get_tec_classes");
				 
			}
		});
		
		/**
		 * 返回监听
		 */
		this.actionBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				StudentSelectClassesActivity.this.finish();
			}
		});
		
		/**
		 * 菜单监听
		 */
		this.actionMenu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				PopupMenuUtil popupMenuUtil=new PopupMenuUtil(v,StudentSelectClassesActivity.this);
				popupMenuUtil.actionBackListener();
				
			}
		});

	}
	
	
	
	
	/**
	 * 获取数据，包括老师，课程，发送指令，查找出所有老师及其对应的课程（stu_get_tec_classes）
	 */
	private void getData(String action) {
		
		List.clear();
		
		RequestParams params = new RequestParams();
		
		params.addBodyParameter("action", action);
		
		HttpUtils httpUtils=new HttpUtils();
		
		httpUtils.send(HttpMethod.POST, URL.ClASS_URL, params, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {

				String jsonString=responseInfo.result;
				
				
				Log.i("MYLog1", jsonString);
				
				setClassData(jsonString);
				 
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				
			}

		});
	}
	
	
	/**
	 * 解析数据
	 * @param jsonString
	 * @return
	 */
		private void setClassData(String jsonString) {
			
			
			ClassTec classtec=null;
			
				if(jsonString!=null){
					
					try {
						
						JSONObject jsonObject = new JSONObject(jsonString);
						
						JSONArray array=jsonObject.getJSONArray("class_tec");
						
						for(int i=0;i<array.length();i++){
							JSONObject json=array.getJSONObject(i);
							
							String class_name=json.getString("class_name");
							String tec_name=json.getString("tec_name");
							
							int class_id=Integer.parseInt(json.getString("class_id"));
							int tec_id=Integer.parseInt(json.getString("tec_id"));
							
							classtec=new ClassTec(class_name, tec_name,MainActivity._id,class_id,tec_id,false);
						
							List.add(classtec);
							
							
							String sql="select data.class_id from data,stu,tec where data.stu_id="+MainActivity._id+" and tec._id=data.tec_id and tec.class_id=data.class_id";
							
							submitCmd(URL.SOCRE_URL, "stu_get_selected_class",sql,List);
							
						}
						
					} catch (JSONException e) {
						e.printStackTrace();
					}
					
				}
				
			}
		
		private void submitCmd(String url,String action,String cmd,final List<ClassTec> List) {
			
			RequestParams params = new RequestParams();
			params.addBodyParameter("action", action);//意图
			params.addBodyParameter("sql",cmd);//表
			
			HttpUtils http = new HttpUtils();
			http.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {

				@Override
				public void onSuccess(ResponseInfo<String> responseInfo) {

					String jsonstring=responseInfo.result;
					
					System.out.println("已选课程"+jsonstring);
					
					if(!("".equals(jsonstring))){
						try {
							JSONObject jsonObject = new JSONObject(jsonstring);
							JSONArray array=jsonObject.getJSONArray("selected_class");
							for(int i=0;i<array.length();i++){
								JSONObject json=array.getJSONObject(i);
								int class_id=Integer.parseInt(json.getString("class_id"));
								
								for(int j = 0; j < List.size(); j ++) {						
									if((List.get(j).getClass_id())==class_id) {
										List.get(j).setChecked(true);
										System.out.println("CLASS_ID:"+List.get(j).getClass_id());
									}
								}
								
								selectClassesAdapter.setData(List);
								selectClassesAdapter.notifyDataSetChanged();
							}
							
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}else if("".equals(jsonstring)){
						
						selectClassesAdapter.setData(List);
						selectClassesAdapter.notifyDataSetChanged();
						
					}
					
				}

				@Override
				public void onFailure(HttpException error, String msg) {
				 
				}

			});
			
		}

	
}
